package com.wootion.cims.module.communityresidence.residencelist.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wootion.cims.module.communityresidence.residencelist.dto.ResidencePageQuery;
import com.wootion.cims.module.communityresidence.residencelist.dto.ResidenceDto;
import com.wootion.cims.module.communityresidence.residencelist.excel.ResidenceExcel;
import com.wootion.cims.module.communityresidence.residencelist.service.ResidenceService;
import com.wootion.cims.module.systemsetting.dictionary.model.LogAnotion;
import com.wootion.cims.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LanQiang
 * @Date: 2022/12/08/16:01
 * @Description: 作用xxx
 */
@RestController
@Api(value = "住宅列表")
@RequiresAuthentication
@RequestMapping(path = {"/residence"})
public class ResidenceController {
    @Resource
    private ResidenceService residenceService;

    /**
     * 查询社区分组信息
     * @return
     */
    @GetMapping(path = {"/group/community"})
    @ApiOperation(value = "社区管理-社区名称分组", httpMethod = "GET",notes = "")
    public ResponseResult groupByCommunity(){
        return residenceService.groupByCommunity();
    }
    /**
     * 根据社区id，查询小区分组信息
     * @return
     */
    @GetMapping(path = {"/group/plot"})
    @ApiOperation(value = "社区管理-小区名称分组", httpMethod = "GET",notes = "communityId=849136005b3b365c7139cd3d27706d51")
    public ResponseResult groupByPlot(@RequestParam(value = "communityId") String communityId){
        return residenceService.groupByPlot(communityId);
    }
    /**
     * 根据小区id和小区类型(楼房，平房/别墅)，查询小区的楼栋和单元信息
     * @return
     */
    @GetMapping(path = {"/group/unit"})
    @ApiOperation(value = "社区管理-楼栋和单元信息", httpMethod = "GET",notes = "")
    public ResponseResult groupByPlotBuilding(@RequestParam(value = "plotId") String plotId,@RequestParam(value = "category")String category){
        return residenceService.groupByPlotBuilding(plotId,category);
    }

    /**
     * 根据小区id和详细地址获取，当前地址下的所有门牌号
     * @param residenceDto
     * @return
     */
    @PostMapping(path = {"/house"})
    @ApiOperation(value = "社区管理-获取住宅门牌号",httpMethod = "POST",notes = "楼房：{\"plotId\":\"11ae70f80d940ad6de2b2bcf4842dda2\",\"addrArray\":[\"1\",\"1\"]} 平房/别墅：{\"plotId\":\"11ae70f80d940ad6de2b2bcf4842dda2\",\"addrArray\":[\"2\"]}")
    public ResponseResult groupByHouseNumber(@RequestBody ResidenceDto residenceDto){
        return residenceService.groupByHouseNumber(residenceDto);
    }
    /**
     * 查询业主信息
     */
    @PostMapping(path = "/person")
    @ApiOperation(value = "社区管理-选择业主", httpMethod = "POST",notes = "")
    public ResponseResult findByPerson(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit,
                                       @RequestParam String condition){
        return residenceService.findByPerson(page,limit,condition);
    }
    /**
     * 新增住宅
     * @return
     */
    @ApiOperation(value = "社区管理-添加住宅信息", httpMethod = "POST",notes = "新增:{\"classes\":\"自有住宅\",\"category\":\"楼房\",\"communityId\":\"2d75693f9a23f7423b6535dfa762f6f1\",\"plotId\":\"3ce5ab0e75757ccb7501f8e2b0ac7187\",\"address\":\"1栋1单元\",\"houseNumber\":\"1-103\",\"ownerId\":\"3\",\"remark\":\"暂时没备注信息\"}" )
    @PostMapping(path = {"/save"})
    public ResponseResult save(@RequestBody ResidenceDto residenceDto, HttpServletRequest request){
        return residenceService.save(residenceDto,request);
    }
    /**
     * 修改住宅
     * @return
     */
    @ApiOperation(value = "修改:{\"id\":\"3\",\"classes\":\"自有住宅\",\"category\":\"楼房\",\"communityId\":\"2d75693f9a23f7423b6535dfa762f6f1\",\"plotId\":\"3ce5ab0e75757ccb7501f8e2b0ac7187\",\"address\":\"1栋1单元\",\"houseNumber\":\"1-104\",\"ownerId\":\"3\",\"remark\":\"暂时没备注信息\"}")
    @PostMapping(path = {"/update"})
    public ResponseResult update(@RequestBody ResidenceDto residenceDto,HttpServletRequest request){
        return residenceService.update(residenceDto,request);
    }
    /**
     * 根据id，删除(批量删除)住宅列表
     * @param jsonObject
     * @return
     */
    @LogAnotion(type = "批量删除",value = "1",incident = "住宅信息",prefixContent = "删除信息")
    @PostMapping(path = {"/delete"})
    @ApiOperation(value = "社区管理-删除(批量删除)住宅列表",httpMethod = "POST",notes = "{'ids':['1','2','3']}")
    public ResponseResult delete(@RequestBody JSONObject jsonObject){
        JSONArray ids = jsonObject.getJSONArray("ids");
        List<String> list = new ArrayList<>();
        for (Object id : ids) {
            list.add(id.toString());
        }
        return residenceService.delete(list);
    }

    /**
     * 查询住宅列表信息/或条件查询,并分页返回
     * @param pageQuery
     * @return
     */
    @PostMapping(path = {"/list"})
    @ApiOperation(value = "社区管理-社区列表查询", httpMethod = "POST",notes = "不是jso格式,而是K-V结构,例如：condition:name page:2 limit:20")
    public ResponseResult findList(@RequestBody ResidencePageQuery pageQuery){
        return residenceService.findListResidence(pageQuery);
    }

    /**
     * 双击查看社区列表中的详情信息
     * @param id
     * @return
     */
    @GetMapping(path = {"/details"})
    @ApiOperation(value = "社区管理-社区详情信息", httpMethod = "GET",notes = "抓宅列表的id:?id=3")
    public ResponseResult findByCommunityId(String id){
        return residenceService.findByCommunityId(id);
    }

    /**
     * 批量添加楼房,平房/别墅
     * @return
     */
    @PostMapping(path = {"/batch/save"})
    @ApiOperation(value = "社区管理-批量添加住宅", httpMethod = "POST",notes = "" +
            "楼房：{\n" +
            "  \"classes\": \"公有住宅\",\n" +
            "  \"category\": \"楼房\",\n" +
            "  \"plotId\": \"11ae70f80d940ad6de2b2bcf4842dda2\",\n" +
            "  \"addrArray\": [\n" +
            "    \"1\",\n" +
            "    \"1\"\n" +
            "  ],\n" +
            "  \"communityId\": \"28934e93c88664552d0ad684a1e3c6ec\",\n" +
            "  \"rules\":{\n" +
            "          \"prefix\": \"A\",\n" +
            "          \"suffix\": \"室\",\n" +
            "          \"floor\": \"1-3\",\n" +
            "          \"room\": \"01-03\"\n" +
            "    }\n" +
            "} " +
            "平房/别墅{\n" +
            "  \"classes\": \"公有住宅\",\n" +
            "  \"category\": \"平房/别墅\",\n" +
            "  \"plotId\": \"0e701bfe42e2f5c0c74243f52310d363\",\n" +
            "  \"addrArray\": [ \"2\" ],\n" +
            "  \"communityId\": \"2d75693f9a23f7423b6535dfa762f6f1\",\n" +
            "  \"rules\":{\n" +
            "          \"prefix\": \"A\",\n" +
            "          \"suffix\": \"区\",\n" +
            "          \"room\": \"001-004\"\n" +
            "    }\n" +
            "}")
    public ResponseResult batchSave(@RequestBody @Validated ResidenceDto residenceDto,HttpServletRequest request){
        return residenceService.batchSave(residenceDto,request);
    }

    /**
     * 导出选中的住宅列表
     * @param residenceExcel
     * @return
     */
    @LogAnotion(type = "导出",value = "1",incident = "住宅信息",prefixContent = "")
    @PostMapping(path = {"/excel"})
    @ApiOperation(value = "社区管理-导出选中住宅", httpMethod = "POST",notes = "{\"ids\":[\"002443e3023366e6d984284134c63525\",\"09dbd05b9356f3cafae3d3bd7ced4b61\"]}")
    public ResponseResult residentialExcel(@RequestBody ResidenceExcel residenceExcel){
        return residenceService.residentialExcel(residenceExcel);
    }
    
}
